﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using HighDivePrint.Model;
using System.Windows.Forms;


namespace MVP_Dive_REGISTER //HighDivePrint.Model.PrintDocument
{
    public class PrintDocumentDive : System.Drawing.Printing.PrintDocument
    {
        /// <summary>
        /// Variabel för att hålla reda på vilken sida jag skriver ut på.
        /// </summary>
        private int _printPageCounter;
        private BindingList<Dive> _list = null;

        /// <summary>
        /// 
        /// </summary>
        public PrintDocumentDive(BindingList<Dive> list)
            : base()
        {
            _list = list;
        }

        public void MyPrint()
        {
            _printPageCounter = 0;

            //System.Windows.Forms.PrintDialog dialog = new System.Windows.Forms.PrintDialog();
            System.Windows.Forms.PrintPreviewDialog dialog = new PrintPreviewDialog();


 //DELETE THE "//"           this.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintOnePage);

            dialog.Document = this;
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.Print(); // Här sker utskriften, fyrar av bl.a EVENT PrintPage
            }
        }

          /// <summary>
        /// Metod för att svara på dokumenthändelsen EVENT PrintPage för att skriva ut nästa sid
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="ev"></param>
       /* private void PrintOnePage(object sender, System.Drawing.Printing.PrintPageEventArgs ev)
        {
            Font font = new Font(FontFamily.GenericMonospace, 12);
            Font printFont = font;
            SolidBrush myBrush = new SolidBrush(Color.Black);

            int linesPerPage = (int)(ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics));
            float yPosition = 0;
            int row = 0;
            float leftMargin = ev.MarginBounds.Left;
            float topMargin = ev.MarginBounds.Top;
            string line = null;
            
            Pen p = new Pen(Color.Red, 4);
            Rectangle r = new Rectangle(0, 0, ev.MarginBounds.Width, ev.MarginBounds.Height);
            //ev.Graphics.DrawEllipse(p, r);
            ev.Graphics.DrawRectangle(p, r);

            row = 1;
            yPosition = topMargin + row * printFont.GetHeight(ev.Graphics);
            ev.Graphics.DrawString("Diver Information", printFont, myBrush, leftMargin, yPosition, new StringFormat());

            row = 3;
            while (row < (linesPerPage - 6))
            {
                line = null;
                int i = _printPageCounter * (linesPerPage - 6) + (row - 3);
                if (i >= _list.Count)
                    break;

                Dive dive = _list[i];
                line = Dive.Diver + " " + Dive.diveCode;

                yPosition = topMargin + row * printFont.GetHeight(ev.Graphics);
                ev.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());

                row++;
            }

            row++;
            yPosition = topMargin + row * printFont.GetHeight(ev.Graphics);
            ev.Graphics.DrawString(_printPageCounter.ToString(), printFont, myBrush, leftMargin, yPosition, new StringFormat());

            // If there are more lines, print another page.
            if (line != null)
                ev.HasMorePages = true;
            else
                ev.HasMorePages = false;
            myBrush.Dispose();

            this._printPageCounter++;
        } */

    
       /* public PrintDocumentDive(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }*/
    }
}
